<%@ taglib prefix="page" uri="http://com.qipay/pagelib" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" import="com.qipay.admin.utils.SiteHelper" contentType="text/html;charset=utf-8" %>
<%@ page import="com.qipay.baas.model.CpAccount" %>
<%@ page import="com.qipay.configuration.config.ConfigurationEnum" %>
<%@ page import="com.qipay.user.entity.UserLevel" %>
<%@ page import="org.apache.commons.collections.CollectionUtils" %>
<%@ page import="org.apache.commons.lang3.StringUtils" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%@ page import="com.qipay.admin.withdraw.vo.*" %>
<!doctype html>
<html>
<head>
    <jsp:include page="head.jsp"></jsp:include>
    <link rel="stylesheet" href="<%= request.getAttribute("StaticHost") %>/css/dataTables.bootstrap.min.css">
    <link rel="stylesheet" href="<%= request.getAttribute("StaticHost") %>/css/bootstrapValidator.min.css">
    <link rel="stylesheet" href="<%= request.getAttribute("StaticHost") %>/css/bootstrap-datepicker3.min.css">

    <script src="<%= request.getAttribute("StaticHost") %>/js/jquery.js"></script>
    <script src="<%= request.getAttribute("StaticHost") %>/js/address.js"></script>
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
    <jsp:include page="nav.jsp"></jsp:include>
    <jsp:include page="left.jsp"></jsp:include>
    <div class="content-wrapper">
        <section class="content">
            <div class="row">
                <div class="col-xs-12">
                    <div class="box box-info">
                        <div class="box-header with-border">
                            <h3 class="box-title">申请</h3>
                        </div>
                        <%
                            UserLevel userLevel = (UserLevel) request.getAttribute("level");
                        %>
                        <%--<div class="box-body" id="dvContent">
                            <div class="box-body">
                                <div class="col-sm-12 col-md-6">
                                    <div class="box-header with-border">
                                        <h3 class="box-title">基础信息</h3>
                                    </div>
                                    <%
                                        CpAccount account = (CpAccount) request.getAttribute("account");
                                        Map<Byte, String> conf = (Map<Byte, String>) request.getAttribute("config");
                                        boolean isHoliday = (boolean) request.getAttribute("isHoliday");
                                    %>
                                    <form class="form-horizontal">
                                        <label for="balance" class="col-sm-5 col-md-5 col-lg-5">账户总余额</label>
                                        <div class="form-group col-sm-7 col-md-7 col-lg-7"><input type="text"
                                                                                                  class="form-control"
                                                                                                  id="balance"
                                                                                                  value="<%= SiteHelper.formatMoney(account.getBalance(), 100.0)%>">
                                        </div>
                                        <label for="available" class="col-sm-5 col-md-5 col-lg-5">账户可提余额</label>
                                        <div class="form-group col-sm-7 col-md-7 col-lg-7"><input type="text"
                                                                                                  class="form-control"
                                                                                                  id="available"

                                                                                                  value="<%=SiteHelper.formatMoney(account.getBalanceAvailable(), 100.0)%>">
                                            <input type="hidden" id="availableVal"
                                                   value="<%=SiteHelper.formatNumber(account.getBalanceAvailable(), 100.0)%>">
                                        </div>
                                        <%if (userLevel == UserLevel.CP) {%>
                                        <label for="advance" class="col-sm-5 col-md-5 col-lg-5">账户垫支余额</label>
                                        <div class="form-group col-sm-7 col-md-7 col-lg-7"><input type="text"
                                                                                                  class="form-control"
                                                                                                  id="advance"
                                                                                                  value="<%=SiteHelper.formatMoney(account.getBalanceAdvance(), 100.0)%>">
                                            <input type="hidden" id="advanceVal"
                                                   value="<%=SiteHelper.formatNumber(account.getBalanceAdvance(), 100.0)%>">
                                        </div>
                                        <%}%>
                                        <label for="disabled" class="col-sm-5 col-md-5 col-lg-5">账户不可提余额</label>
                                        <div class="form-group col-sm-7 col-md-7 col-lg-7"><input type="text"
                                                                                                  class="form-control"
                                                                                                  id="disabled"
                                                                                                  value="<%=SiteHelper.formatMoney(account.getBalanceDisabled(), 100.0)%>">
                                            <input type="hidden" id="disabledVal"
                                                   value="<%=SiteHelper.formatNumber(account.getBalanceDisabled(), 100.0)%>">
                                        </div>
                                        <label hidden for="min" class="col-sm-5 col-md-5 col-lg-5">最小金额</label>
                                        <div hidden class="form-group col-sm-7 col-md-7 col-lg-7"><input type="text"
                                                                                                         class="form-control"
                                                                                                         id="min"

                                                                                                         value="<%=SiteHelper.formatMoney(conf.get(ConfigurationEnum.WITHDRAW_MIN.getType()))%>">
                                            <input type="hidden" id="minVal"
                                                   value="<%=SiteHelper.formatNumber(conf.get(ConfigurationEnum.WITHDRAW_MIN.getType()))%>">
                                        </div>
                                        <label hidden for="max" class="col-sm-5 col-md-5 col-lg-5">最大金额</label>
                                        <div hidden class="form-group col-sm-7 col-md-7 col-lg-7"><input type="text"
                                                                                                         class="form-control"
                                                                                                         id="max"

                                                                                                         value="<%=SiteHelper.formatMoney(isHoliday? conf.get(ConfigurationEnum.WITHDRAW_MAX_HOLIDAY.getType()): conf.get(ConfigurationEnum.WITHDRAW_MAX_WORK.getType()))%>">
                                            <input type="hidden" id="maxVal"
                                                   value="<%=
                                                   SiteHelper.formatNumber(
                                                           isHoliday
                                                    ? conf.get(ConfigurationEnum.WITHDRAW_MAX_HOLIDAY.getType())
                                                    : conf.get(ConfigurationEnum.WITHDRAW_MAX_WORK.getType()))%>">
                                        </div>
                                        <label hidden for="cost" class="col-sm-5 col-md-5 col-lg-5">可用余额-固定收费</label>
                                        <div hidden class="form-group col-sm-7 col-md-7 col-lg-7"><input type="text"
                                                                                                         class="form-control"
                                                                                                         id="cost"

                                                                                                         value="<%=SiteHelper.formatMoney(conf.get(ConfigurationEnum.WITHDRAW_AVAILABLE_COST.getType()))%>">
                                            <input type="hidden" id="costVal"
                                                   value="<%=SiteHelper.formatNumber(conf.get(ConfigurationEnum.WITHDRAW_AVAILABLE_COST.getType()))%>">
                                        </div>
                                        <%if (userLevel == UserLevel.CP) {%>
                                        <label hidden for="rate" class="col-sm-5 col-md-5 col-lg-5">垫支余额-收费利率</label>
                                        <div hidden class="form-group col-sm-7 col-md-7 col-lg-7"><input type="text"
                                                                                                         class="form-control"
                                                                                                         id="rate"

                                                                                                         value="<%=conf.get(ConfigurationEnum.WITHDRAW_ADVANCE_RATE.getType())%>">
                                            <input type="hidden" id="rateVal"
                                                   value="<%=SiteHelper.formatNumber(conf.get(ConfigurationEnum.WITHDRAW_ADVANCE_RATE.getType()))%>">
                                        </div>
                                        <%}%>
                                        <input type="hidden" id="password"
                                               value="<%=request.getAttribute("password")%>">
                                        <input type="hidden" id="start"
                                               value="<%=conf.get(ConfigurationEnum.AUDIT_START_TIME.getType())%>">
                                        <input type="hidden" id="end"
                                               value="<%=conf.get(ConfigurationEnum.AUDIT_END_TIME.getType())%>">
                                    </form>
                                </div>
                            </div>
                        </div>--%>
                        <div class="box">
                            <%
                                WithdrawVo detail = (WithdrawVo) request.getAttribute("detail");

                                String validCode = (String) request.getAttribute("validCode");
                            %>
                            <div class="box-body">
                                <form class="form-horizontal" action="/withdraw/withdrawSubmit" method="POST" id="applyForm">
                                    <%--隐藏值始--%>
                                   <%-- <input type="hidden" class="form-control" id="id" name="id"
                                           value="<%=detail.getId()%>">
                                    <input type="hidden" class="form-control" id="operation"
                                           name="operation" value="<%=detail.getOperation()%>">
                                    <input type="hidden" class="form-control" id="type"
                                           name="type" value="<%=detail.getType()%>">--%>
                                    <%--隐藏值止--%>

                                    <label for="operationName" class="col-sm-2">商户交易流水号</label>
                                    <div class="form-group col-sm-10">
                                        <div class="col-sm-10 no-padding">
                                            <input type="text" class="form-control"
                                                   id="operationName" name="operationName"
                                                   placeholder="商户交易流水号"
                                                   value="">
                                        </div>
                                        <div class="col-sm-2 vertical-align: middle">
                                            <button onclick="renderOrderNumber.apply(window)">自动生成</button>
                                        </div>
                                    </div>

                                    <label class="col-sm-2" for="orderNo">商家订单号</label>
                                    <div class="form-group col-sm-10"><input type="text" class="form-control"
                                                                             id="orderNo"
                                                                             name="name"
                                                                             placeholder="商家订单号"
                                                                             value="">
                                    </div>

                                    <label class="col-sm-2" >银行</label>
                                    <div class="form-group col-sm-10">
                                        <select id="banks" name="cardBank" onchange="findSunBank();" class="form-control">
                                            <option value="招商银行">招商银行</option>
                                            <option value="中国工商银行">中国工商银行</option>
                                            <option value="中国建设银行">中国建设银行</option>
                                            <option value="浦发银行">浦发银行</option>
                                            <option value="中国农业银行">中国农业银行</option>
                                            <option value="中国民生银行">中国民生银行</option>
                                            <option value="平安银行">平安银行</option>
                                            <option value="兴业银行">兴业银行</option>
                                            <option value="交通银行">交通银行</option>
                                            <option value="中信银行">中信银行</option>
                                            <option value="中国光大银行">中国光大银行</option>
                                            <option value="华夏银行">华夏银行</option>
                                            <option value="中国银行">中国银行</option>
                                            <option value="广发银行">广发银行</option>
                                            <option value="邮政储蓄银行">邮政储蓄银行</option>
                                            <option value="其他银行">其他银行</option>
                                        </select>
                                    </div>

                                    <label class="col-sm-2" for="withdrawNo">扣款账号</label>
                                    <div class="form-group col-sm-10"><input type="text" class="form-control"
                                                                             id="withdrawNo"
                                                                             name="cardNo"
                                                                             placeholder="扣款账号"
                                                                             value="">
                                    </div>

                                    <label class="col-sm-2"  for="cardType">账号类型</label>
                                    <div class="form-group col-sm-10">
                                        <select id="cardType" name="cardBank" onchange="findSunBank();" class="form-control">
                                            <option value="个人借记卡账户">个人借记卡账户</option>
                                            <option value="个人贷记卡帐户">个人贷记卡帐户</option>
                                        </select>
                                    </div>

                                    <label class="col-sm-2" for="name">姓名</label>
                                    <div class="form-group col-sm-10"><input type="text" class="form-control"
                                                                             id="name"
                                                                             name="name"
                                                                             placeholder="姓名"
                                                                             value="">
                                    </div>

                                    <label class="col-sm-2" for="deductmoney">扣款金额</label>
                                    <div class="form-group col-sm-10"><input type="text" class="form-control"
                                                                             id="deductmoney"
                                                                             name="deductmoney"
                                                                             value="0">
                                    </div>

                                    <label class="col-sm-2"  for="usercardType">付款人证件类型</label>
                                    <div class="form-group col-sm-10">
                                        <select id="usercardType" name="cardBank" onchange="findSunBank();" class="form-control">
                                            <option value="身份证">身份证</option>
                                            <option value="户口本">户口本</option>
                                        </select>
                                    </div>

                                    <label class="col-sm-2"  for="usercardNO">付款人证件号码</label>
                                    <div class="form-group col-sm-10">
                                        <input type="text" class="form-control"
                                               id="usercardNO"
                                               name="usercardNO"
                                               value="">
                                    </div>

                                    <label class="col-sm-2" for="twoLevelUser">二级商户号</label>
                                    <div class="form-group col-sm-10">
                                        <input type="text" class="form-control"
                                               id="twoLevelUser"
                                               name="twoLevelUser"
                                               value="">
                                    </div>

                                    <label class="col-sm-2" for="telephone">手机号码</label>
                                    <div class="form-group col-sm-10">
                                        <input type="text" class="form-control"
                                               id="telephone"
                                               name="telephone"
                                               value="  ">
                                    </div>

                                    <label class="col-sm-2" for="validityDate">有效期(MMYY)</label>
                                    <div class="form-group col-sm-10">
                                        <input type="text" class="form-control"
                                               id="validityDate"
                                               name="validityDate"
                                               value="">
                                    </div>

                                    <label class="col-sm-2" for="cvv2">CVV2</label>
                                    <div class="form-group col-sm-10 date form_datetime">
                                        <input type="text" class="form-control "
                                               id="cvv2"
                                               name="cvv2"
                                               value="">
                                    </div>

                                    <label class="col-sm-2" for="use">用途</label>
                                    <div class="form-group col-sm-10">
                                        <select id="use" name="cardBank" onchange="findSunBank();" class="form-control">
                                            <option value="服务费">服务费</option>
                                            <option value="户口本">手续费</option>
                                            <option value="办公费">办公费</option>
                                        </select>
                                    </div>

                                    <div class="box-footer">
                  <%--                      <button type="button" class="btn btn-default" onclick="window.history.back()">
                                            返回
                                        </button>--%>
                                        <button type="submit" class="btn btn-primary" id="btnSubmit">提交</button>
                                        <p class="message text-danger">
                                        </p>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
    </div>
    </section>

</div>
<jsp:include page="foot.jsp"></jsp:include>
</div>

<script src="<%= request.getAttribute("StaticHost") %>/js/adminlte.min.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/icheck.min.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/bootstrapValidator.min.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/business/withdraw_apply.js?v=<%= request.getAttribute("StaticVersion") %>"></script>
<script type="text/javascript">
     //查询银行
    function findSunBank() {
        var province = $("#province").val();
        var city = $("#city").val();
        var bank = $("#banks").val();
        //alert(province+":"+city+":"+bank);
        $.ajax({
            url: "${pageContext.request.contextPath}/ajaxBanks?province=" + province+"&city="+city+"&bank="+bank,
            type: "GET",
            async: true,//true异步 false同步
            context: this,
            timeout: 1000000,
            success: function (data) {
                var code0 = data.code;
                if (code0 != null && code0 == 200) {
                    var banks = data.data;
                    $('#cardSubBank').empty();
                    for(var i=0;i<banks.length;i++){
                        $('#cardSubBank').append('<option value="'+banks[i].sub_branch_name+'">'+banks[i].sub_branch_name+'</option>');
                        //alert($('#cities').find("option").length);
                    }
                    //alert(citys.length);

                }
                else {
                    $('#cardSubBank').empty();
                    //alert("未查询到相关支行数据");
                }
            },
            error: function (d, s, b) {
                alert("失败:" + b);
            }
        });


    }


    $(function () {

        // init.args
        var operation = $("#operation_tab li.active").attr("key");
        var password = $("#password").val();
        var min = Number($("#minVal").val());
        var max = Number($("#maxVal").val());
        console.log(min);
        console.log($("#maxVal").val() + "= " + max);
        var start = $("#start").val();
        var end = $("#end").val();
        var code = "";
        type = $(".tab-content div.active .type").find("option:selected").attr("type");

        // init.fill
        $("#type").val(type);
        $("#operation").val(operation);
        $("#operationName").val($("#operation_tab li.active a").text());
        initAmount();

        $('#validityDate').datepicker({
            format: "mmyy",
            todayBtn: true,
            clearBtn: true,
            language: "zh-CN"
        });

        if(<%= request.getAttribute("addSuccess") %>){
            alert("新增成功");
        }

        // init.validator
        $('#applyForm').bootstrapValidator({
            framework: 'bootstrap',
            icon: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                operationName: {
                    validators: {
                        notEmpty: {
                            message: '商户交易流水号不能为空'
                        }
                    }
                },
                amount: {
                    validators: {
                        notEmpty: {
                            message: '操作金额不能为空'
                        },
                        // callback: {
                        //     message: '操作金额不足',
                        //     callback: function (value, validator) {
                        //
                        //         var available = Number($("#availableVal").val());
                        //         var advance = Number($("#advanceVal").val());
                        //         return value <= (0 == type ? available : advance);
                        //     }
                        // },
                        // between: {
                        //     min: min,
                        //     max: max,
                        //     message: '操作金额范围为' + min + '到' + max
                        // }
                    }
                },
                fee: {
                    validators: {
                        notEmpty: {
                            message: '有效期不能为空'
                        }
                    }
                },
                actualAmount: {
                    validators: {
                        notEmpty: {
                            message: '实际到账金额不能为空'
                        }
                    }
                },
                cardBank: {
                    validators: {
                        notEmpty: {
                            message: '银行名称不能为空'
                        }
                    }
                },
                cardSubBank: {
                    validators: {
                        notEmpty: {
                            message: '支行名称不能为空'
                        }
                    }
                },
                cardNo: {
                    validators: {
                        notEmpty: {
                            message: '扣款账号不能为空'
                        }, stringLength: {
                            min: 16,
                            max: 19,
                            message: '银行卡位数有误'
                        },
                    }
                },
                idCardNo: {
                    validators: {
                        notEmpty: {
                            message: '扣款账号不能为空'
                        }, stringLength: {
                            min: 17,
                            max: 18,
                            message: '身份证位数有误'
                        },
                    }
                },
                name: {
                    validators: {
                        notEmpty: {
                            message: '姓名不能为空'
                        }
                    }
                },
                // validCode: {
                //     validators: {
                //         notEmpty: {
                //             message: '手机校验码不能为空'
                //         },
                //         callback: {
                //             message: '手机校验码错误',
                //             callback: function (value, validator) {
                //                 return code == value;
                //             }
                //         },
                //     }
                // },
                withdrawPassword: {
                    validators: {
                        notEmpty: {
                            message: '提现密码不能为空'
                        }
                        // ,
                        // callback: {
                        //     message: '提现密码错误',
                        //     callback: function (value, validator) {
                        //         return password == value;
                        //     }
                        // },
                    },
                    regexp: {
                        regexp: /^[0-9]{6}$/,
                        message: '提现密码位数有误'
                    }
                },
            }
        });

        // sms
        $("#sendValid").click(function () {
            //看是否在两分钟之内，有没有发送验证码，如果有，则拒绝发送---服务端查询。
            $.ajax({
                url: "/withdraw/queryValid",
                type: "PUT",
                async: true,
                context: this,
                timeout: 50000,
                success: function (data) {
                    if (null != data && data.code == 200) {
                        var limit = 60;
                        $("#sendValid").attr("disabled", true);
                        var timer = setInterval(function () {
                            if (--limit != 0) {
                                console.log(limit)
                                $("#sendValid").text(limit + "秒后重发");
                            } else {
                                console.log("60s pass")
                                $("#sendValid").text("发送校验码");
                                $("#sendValid").attr("disabled", false);
                                clearInterval(timer);
                            }
                        }, 1000);
                        // request
                        $.get("/withdraw/valid", function (data) {
                            if (null != data && data.code == 200) {
                                code = data.data
                            } else {
                                console.log("get fail")
                                clearInterval(timer);
                                $("#sendValid").attr("disabled", false);
                                $("#sendValid").text("发送校验码");
                                alert("失败:" + data.msg);
                            }
                        }).fail(function () {
                            console.log("get request fail")
                            clearInterval(timer);
                            $("#sendValid").attr("disabled", false);
                            $("#sendValid").text("发送校验码");
                        });
                    } else {
                        alert("操作过于频繁，提现操作限定为2分钟一次噢");
                    }
                },
                error: function (d, s, b) {
                    //alert("失败:" + b);
                    $('#banlance').val("0");
                }
            });


        });
    });

    function checkTime() {
        // curTime
        var date = new Date();
        var start = $("#start").val();
        var end = $("#end").val();
        var curTime = date.getHours() * 100 + date.getMinutes();
        var startLimit = parseFloat(start.replace(":", ""));
        var endLimit = parseFloat(end.replace(":", ""));
        // check
        return curTime > startLimit && curTime < endLimit
    }

     /**
      * 点击随机生成按钮
      */
    function renderOrderNumber(){
        var orderNo= randomNumber();
        $("#operationName").val(orderNo);
     }

     //根据当前时间和随机数生成流水号
     function randomNumber() {
         var outTradeNo="";  //订单号
         for(var i=0;i<6;i++) //6位随机数，用以加在时间戳后面。
         {
             outTradeNo += Math.floor(Math.random()*10);
         }
         outTradeNo = new Date().getTime() + outTradeNo;  //时间戳，用来生成订单号。
         return outTradeNo;
     }


</script>
<script src="<%= request.getAttribute("StaticHost") %>/js/bootstrap-datepicker.min.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/bootstrap-datepicker.zh-CN.min.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/adminlte.min.js"></script>

</body>
</html>

